﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoDownload.Extension
{
    public static class Extension
    {
        public static string UpperFirstLetter(this string source)
        {
            var before = ' ';
            var builder = new StringBuilder(source);

            for (var i = 0; i < builder.Length; i++)
            {
                if (Char.IsLetterOrDigit(builder[i]) && !Char.IsLetterOrDigit(before))
                    builder[i] = Char.ToUpper(builder[i]);
                else
                    builder[i] = char.ToLower(builder[i]);

                before = builder[i];
            }

            return builder.ToString();
        }

        public static string TrimAll(this string source)
        {
            int i;
            var builder = new StringBuilder(source.Length);

            for (i = 0; i < source.Length - 1; i++)
            {
                if (Char.IsLetterOrDigit(source[i]))
                    builder.Append(source[i]);
                else if ((Char.IsLetterOrDigit(source[i + 1]) && builder.Length > 0))
                    builder.Append(' ');
            }

            if (source.Length > 0 && Char.IsLetterOrDigit(source[source.Length - 1]))
                builder.Append(source[source.Length - 1]);

            return builder.ToString();
        }

        public static void TestObjectInitialize()
        {
            var sb = new StringBuilder { Length = 20 };
        }
    }
}
